home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Flip / getopt.c < prev    next >
C/C++ Source or Header  |  1995-06-12  |  3KB  |  99 lines

  1. /* ::[[ @(#) getopt.c 1.5 89/07/02 00:18:19 ]]:: */
  2. #ifndef LINT
  3. static char sccsid[]="::[[ @(#) getopt.c 1.5 89/07/02 00:18:19 ]]::";
  4. #endif
  5.  
  6. /*
  7. Checksum:  505161377      (check or update this with "brik")
  8. */
  9.  
  10. /*
  11.  * Here's something you've all been waiting for:  the AT&T public domain
  12.  * source for getopt(3).  It is the code which was given out at the 1985
  13.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  14.  * directly from AT&T.  The people there assure me that it is indeed
  15.  * in the public domain.
  16.  *
  17.  * There is no manual page.  That is because the one they gave out at
  18.  * UNIFORUM was slightly different from the current System V Release 2
  19.  * manual page.  The difference apparently involved a note about the
  20.  * famous rules 5 and 6, recommending using white space between an option
  21.  * and its first argument, and not grouping options that have arguments.
  22.  * Getopt itself is currently lenient about both of these things White
  23.  * space is allowed, but not mandatory, and the last option in a group can
  24.  * have an argument.  That particular version of the man page evidently
  25.  * has no official existence, and my source at AT&T did not send a copy.
  26.  * The current SVR2 man page reflects the actual behavor of this getopt.
  27.  * However, I am not about to post a copy of anything licensed by AT&T.
  28.  */
  29.  
  30. /*
  31. Minor modifications by Rahul Dhesi 1989/03/06
  32. */
  33.  
  34. #include "flip.h"
  35.  
  36. #ifdef STDINCLUDE
  37. # include <string.h>
  38. #else
  39. extern int strcmp PARMS ((char *, char *));
  40. extern char *strchr PARMS ((char *, char));
  41. #endif
  42.  
  43. /* Avoid possible compiler warning if we simply redefine NULL or EOF */
  44. #define XNULL   0
  45. #define XEOF (-1)
  46.  
  47. #define ERR(szz,czz) if(opterr){fprintf(stderr,"%s%s%c\n",argv[0],szz,czz);}
  48.  
  49. int   opterr = 1;
  50. int   optind = 1;
  51. int   optopt;
  52. char  *optarg;
  53.  
  54. int
  55. getopt(argc, argv, opts)
  56. int   argc;
  57. char  **argv, *opts;
  58. {
  59.    static int sp = 1;
  60.    register int c;
  61.    register char *cp;
  62.  
  63.    if(sp == 1)
  64.       if(optind >= argc ||
  65.          argv[optind][0] != '-' || argv[optind][1] == '\0')
  66.          return(XEOF);
  67.       else if(strcmp(argv[optind], "--") == XNULL) {
  68.          optind++;
  69.          return(XEOF);
  70.       }
  71.    optopt = c = argv[optind][sp];
  72.    if(c == ':' || (cp=strchr(opts, c)) == XNULL) {
  73.       ERR(": illegal option -- ", c);
  74.       if(argv[optind][++sp] == '\0') {
  75.          optind++;
  76.          sp = 1;
  77.       }
  78.       return('?');
  79.    }
  80.    if(*++cp == ':') {
  81.       if(argv[optind][sp+1] != '\0')
  82.          optarg = &argv[optind++][sp+1];
  83.       else if(++optind >= argc) {
  84.          ERR(": option requires an argument -- ", c);
  85.          sp = 1;
  86.          return('?');
  87.       } else
  88.          optarg = argv[optind++];
  89.       sp = 1;
  90.    } else {
  91.       if(argv[optind][++sp] == '\0') {
  92.          sp = 1;
  93.          optind++;
  94.       }
  95.       optarg = XNULL;
  96.    }
  97.    return(c);
  98. }
  99.